<%@page import="system.Html"%>
<%@page import="models.Pesanan"%>
<%
    ArrayList<Pesanan> pesanans = (ArrayList<Pesanan>) request.getAttribute("pesanans");    
%>

<h1>Kelola Pesanan</h1>

<div id="kelola-pesanan-container">
    <div class="content_menu">
        <a href="<%=request.getContextPath()%>/kelolaPesanan/tiba">Pesanan Tiba</a>
        <a href="<%=request.getContextPath()%>/kelolaPesanan/dibayar">Pesanan Dibayar</a>
        <a href="<%=request.getContextPath()%>/kelolaPesanan/dikonfirmasi">Pesanan Dikonfirmasi</a>
        <a href="<%=request.getContextPath()%>/kelolaPesanan/baru">Pesanan Baru</a>
    </div>
    <div id="kelola-pesanan-content">
        <%if (pesanans != null && pesanans.size() > 0) {%>
        <table>
            <thead>
                <tr>
                    <th>No. Order</th><th>Waktu Order</th><th>Pemesan</th><th>Status</th>
                    <% if("/kelolaPesanan/baru".equals(request.getAttribute("servletPath"))) { %>
                    <th>Aksi</th>
                    <% } %>
                </tr>
            </thead>
            <tbody>
                <%for(Pesanan p : pesanans){%>
                <tr>
                    <td><%=p.id%></td>
                    <td><%=p.tanggal%></td>
                    <td><%=p.getUser().nama_lengkap%></td>
                    <td>
                        <form method="POST" action="<%=request.getContextPath()+request.getAttribute("servletPath")%>">
                            <input type="hidden" name="pesanan_id" value="<%=p.id%>" />
                            <select name="status" class="set-status">
                                <option <%= (Pesanan.STATUS_NEW == p.status) ? "selected" : "" %> value="<%= Pesanan.STATUS_NEW %>">Baru</option>
                                <option <%= (Pesanan.STATUS_CONFIRM_PAY == p.status) ? "selected" : "" %> value="<%= Pesanan.STATUS_CONFIRM_PAY %>">Dikonfirmasi</option>
                                <option <%= (Pesanan.STATUS_PAY == p.status) ? "selected" : "" %> value="<%= Pesanan.STATUS_PAY %>">Dibayar</option>
                                <option <%= (Pesanan.STATUS_ARRIVED == p.status) ? "selected" : "" %> value="<%= Pesanan.STATUS_ARRIVED %>">Tiba</option>
                            </select>
                        </form>
                    </td>
                    <td>
                    <% if("/kelolaPesanan/baru".equals(request.getAttribute("servletPath"))) { %>
                    <a href="<%=request.getContextPath()+"/kelolaPesanan/cancel?id="+p.id%>"><button>Batalkan</button></a>
                    <% } %>
                    <a href="<%=request.getContextPath()+"/kelolaPesanan/view?id="+p.id%>"><button>Detail</button></a>
                    </td>
                </tr>
                <%}%>
            </tbody>
        </table>
        <%} else {%>
        Pesanan tidak ditemukan.
        <%}%>
    </div>
</div>

<script type="text/javascript">
    $(".set-status").change(function(){
        $($(this).parent()).submit();
    })
</script>